<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        type="danger"
        round
        plain
        size="mini"
        class="edit-btn"
        @click="isEdit = !isEdit"
        >{{ !isEdit ? '编辑' : '完成' }}</van-button
      >
    </van-cell>
    <van-grid :gutter="10" class="my-grid">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in channels"
        :key="index"
        @click="onMyChannelClick(channel, index)"
      >
        <van-icon
          v-show="isEdit && !fiexChannel.includes(channel.id)"
          slot="icon"
          name="close"
        ></van-icon>
        <span class="text" :class="{ active: index === active }" slot="text">{{
          channel.name
        }}</span>
      </van-grid-item>
    </van-grid>
    <!-- /我的频道 -->
    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10" class="recommend-grid">
      <van-grid-item
        class="grid-item"
        v-for="channel in recommendChannels"
        :key="channel.id"
        icon="plus"
        :text="channel.name"
        @click="onAddChannel(channel)"
      />
    </van-grid>
    <!-- /频道推荐 -->
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel
} from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/store/storage'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      channelLists: [],
      isEdit: false,
      fiexChannel: [0] // 固定频道，不允许删除
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels() {
      const channel = []
      this.channelLists.forEach(item => {
        const res = this.channels.find(myChannel => {
          return myChannel.id === item.id
        })
        if (!res) {
          channel.push(item)
        }
      })
      return channel
    },
    recommendChannels2() {
      return this.channelLists.filter(item => {
        return !this.channels.find(item2 => item2.id === item.id)
      })
    }
  },
  watch: {},
  created() {
    this.loadAllChannels()
  },
  mounted() {},
  methods: {
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels()
        console.log('获取所有频道', data)
        this.channelLists = data.data.channels
      } catch (error) {}
    },
    async onAddChannel(channel) {
      this.channels.push(channel)
      if (this.user) {
        // 已登录
        try {
          await addUserChannel({
            id: channel.id,
            seq: this.channels.length
          })
          this.$toast('添加成功')
        } catch (error) {
          this.$toast('保存失败，稍后重试！')
        }
      } else {
        // 未登录
        setItem('TOUTIAO-CHANNELS', this.channels)
      }
    },
    onMyChannelClick(channel, index) {
      if (this.isEdit) {
        if (this.fiexChannel.includes(channel.id)) {
          return
        }
        if (this.active > index) {
          this.$emit('update:active', this.active - 1) // 更新active 父组件.sync
        }
        // 删除频道项
        this.channels.splice(index, 1)
        // 处理持久化
        this.deleteChannel(channel)
      } else {
        this.$emit('update-active', index)
        this.$emit('update-popup')
      }
    },
    async deleteChannel(channel) {
      try {
        if (this.user) {
          // 已登录，更新到线上
          await deleteUserChannel(channel.id)
          this.$toast('删除成功')
        } else {
          // 未登录
          setItem('TOUTIAO-CHANNELS', this.channels)
        }
      } catch (error) {
        this.$toast('删除失败，稍后重试！')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333;
  }
  .edit-btn {
    width: 104px;
    height: 48px;
    line-height: 48px;
    font-size: 26px;
    color: #ff6758;
    border: 1px solid #ff6758;
  }
  /deep/.grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f3f5f7;
      .van-grid-item__text,
      .text {
        color: #222;
        font-size: 28px;
        margin-top: 0;
      }
      .active {
        color: red;
      }
    }
  }
  /deep/ .my-grid {
    .grid-item {
      .van-grid-item__icon-wrapper {
        position: unset;
      }
      .van-icon-close {
        position: absolute;
        right: -5px;
        top: -5px;
        font-size: 15px;
        z-index: 2;
        color: #cfcdcf;
      }
    }
  }
  /deep/.recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
        .van-grid-item__text {
          line-height: unset;
        }
      }
    }
  }
}
</style>
